package com.chrisandjo.rostra.dao.jpa;

import com.chrisandjo.rostra.dao.RosterDao;
import com.chrisandjo.rostra.domain.Roster;
import com.chrisandjo.rostra.domain.RosterEntity;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import java.util.List;

public class RosterDaoImpl extends JpaDaoSupport implements RosterDao {
    private static final String ROSTER_ENTITY = "RosterEntity";

    public void save(Roster roster) {
        getJpaTemplate().persist(roster);
    }

    public boolean rosterExists(String rosterName) {
        List list = getJpaTemplate().find("select r from " +
                ROSTER_ENTITY +
                " r where r.name = '" + rosterName + "'");
        return !list.isEmpty();
    }

    public Roster getRosterById(long id) {
        return getJpaTemplate().find(RosterEntity.class, id);
    }

    @SuppressWarnings({"unchecked"})
    public List<Roster> getRosters() {
        return getJpaTemplate().find("select r from " + ROSTER_ENTITY + " r");
    }
}
